package com.fundcrawler.service.manager;

import com.fundcrawler.common.Constants;
import com.fundcrawler.entity.BizParam;
import com.fundcrawler.repository.BizParamRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class BizParamManager {
    @Autowired
    private BizParamRepository bizParamRepository;

    public BizParam findByParamCode(String paramCode){
        return findByParamCodeGroup(paramCode, Constants.DEFAULT_PARAM_GROUP);
    }

    public List<BizParam> findAllByParamCode(String paramCode){
        return bizParamRepository.findAllByParamCode(paramCode);
    }

    public BizParam findByParamCode(String paramCode, BizParam defaultIfNull){
        return findByParamCodeGroup(paramCode, Constants.DEFAULT_PARAM_GROUP, defaultIfNull);
    }

    public BizParam findByParamCodeGroup(String paramCode, String paramGroup){
        return findByParamCodeGroup(paramCode, paramGroup, null);
    }

    public BizParam findByParamCodeGroup(String paramCode, String paramGroup, BizParam defaultIfNull){
        BizParam bizParam = bizParamRepository.findByParamCodeGroup(paramCode, paramGroup);
        return bizParam==null? defaultIfNull:bizParam;
    }

    public String findByParamCodeGroupDefault(String paramCode, String paramGroup, String defaultParamValue){
        BizParam bizParam = bizParamRepository.findByParamCodeGroup(paramCode, paramGroup);
        return bizParam==null? defaultParamValue:bizParam.getParamValue();
    }


}
